package com.bwie.controller;


import com.bwie.bean.Essay;
import com.bwie.bean.Tag;
import com.bwie.service.EssayService;
import com.bwie.util.FileUploadUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class EssayController {

    @Resource
    private EssayService essayService;



    @RequestMapping("list.do")
    public String list(
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            String  title,
            String  content,
            String  tname,
            Model model){

        Map map = new HashMap();
        map.put("title",title);
        map.put("content",content);
        map.put("tname",tname);

        PageHelper.startPage(pageNum,3);
        List list= essayService.list(map);

        PageInfo pageInfo = new PageInfo(list);

        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("map",map);
       return "list";
    }

    @RequestMapping("addto.do")
    public String addto(){
        return "add";
    }

    @RequestMapping("listtag.do")
    @ResponseBody
    public Object listtag(){
       List<Tag> tags= essayService.listtag();
       return tags;
    }

    @RequestMapping("add.do")
    @ResponseBody
    public Object add(Essay essay, HttpSession session,
                      @RequestParam("photo") MultipartFile photo,Integer tid){
        if (photo.getSize()>0){
            String upload = FileUploadUtil.load(session, photo, "upload");
            essay.setImg(upload);
        }
        int i=essayService.add(essay,tid);
        return i;
    }

    @RequestMapping("updato.do")
    public String updato(){
        return "upda";
    }

    @RequestMapping("del.do")
    @ResponseBody
    public Object del(Integer eid){

       int i= essayService.del(eid);
       return i;
    }

    @RequestMapping("upda.do")
    @ResponseBody
    public Object upda(Essay essay, HttpSession session,
                      @RequestParam("photo") MultipartFile photo,Integer tid){
        if (photo.getSize()>0){
            String upload = FileUploadUtil.load(session, photo, "upload");
            essay.setImg(upload);
        }
        int i=essayService.upda(essay,tid);
        return i;
    }

    @RequestMapping("hui.do")
    @ResponseBody
    public Object hui(Integer eid){
       Essay essay= essayService.hui(eid);
       return essay;
    }

    @RequestMapping("dels.do")
    @ResponseBody
    public Object dels(String eids){
        int i=essayService.dels(eids);
        return i;
    }


}







































